//
//  GroupInfoViewController.m
//  AAIM
//
//  Created by awh on 2019/12/8.
//  Copyright © 2019年 AAIM. All rights reserved.
//

#import "GroupInfoViewController.h"
#import "ContactRequestLogic.h"
#import "ChatViewController.h"
@interface GroupInfoViewController ()
@property(nonatomic,strong)UIView * backView;
@property(nonatomic,strong)UIImageView * headimage;
@property(nonatomic,strong)UILabel * namelbl;
@property(nonatomic,strong)UIImageView * QRCodeImage;
@property(nonatomic,strong)UILabel * titlelbl;

@property(nonatomic,strong)UILabel * groupMangerlbl;
@property(nonatomic,strong)UIImageView * groupMangerHead;
@property(nonatomic,strong)UILabel * groupMangerName;

@property(nonatomic,strong)UIButton*ConfirmJoinButton;
@end

@implementation GroupInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle=@"群信息";
    
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1.0];
    
    [self.view addSubview:self.backView];
    [self.view addSubview:self.headimage];
    [self.backView addSubview:self.namelbl];
    [self.backView addSubview:self.titlelbl];
    [self.backView addSubview:self.groupMangerlbl];
    [self.backView addSubview:self.groupMangerHead];
    [self.backView addSubview:self.groupMangerName];
    [self.backView addSubview:self.ConfirmJoinButton];
    if ([_pageType isEqualToString:@"searchPageTo"]) {
        [self dataSet];
    }else if ([_pageType isEqualToString:@"codePage"]){
        [self getGroupInfo];
    }
}

- (void)getGroupInfo{
    KWeakSelf;
    [ContactRequestLogic requestGroupCodePUshWithGroupCode:_groupID Success:^(NSDictionary * _Nonnull Dict) {
        NSLog(@"群二维码：%@",Dict);
        if ([Dict[@"code"] integerValue] == 200 && ![CommonTools isNull:Dict key:@"data"]) {
            NSDictionary * dic = Dict[@"data"];
            weakSelf.groupModel = [ContactModel mj_objectWithKeyValues:dic];
            [weakSelf dataSet];
        }
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}

- (void)dataSet{
    KWeakSelf;
    [_headimage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,_groupModel.groupHead]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        
        weakSelf.headimage.image = image;
    }];
    _namelbl.text = _groupModel.groupName;
    _groupMangerName.text = _groupModel.nickname;
    
    [_groupMangerHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,_groupModel.head]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        weakSelf.groupMangerHead.image = image;
    }];
}

- (void)joinGroup:(UIButton *)sender{
    [ContactRequestLogic requestJoinGroupSetWithGroupId:_groupModel.user_id GroupCode:@"" fuuids:[LoginManager shareLoginManager].userModel.fuuid reason:@"" type:@"3" Success:^(NSDictionary * _Nonnull Dict) {
//        NSLog(@"%@",Dict);
        KWeakSelf;
//        NSLog(@"加入结果%@",Dict[@"message"]);
//
        if ([Dict[@"code"] integerValue] == 200) {

            [self showTipHudWithText:@"申请成功，等待群主审核"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });
           
        }
        
    } failure:^(NSString * _Nonnull errMsg) {
        [self showTipHudWithText:errMsg];
    }];
}

-(UIView* )backView
{
    if (!_backView) {
        _backView=[[UIView alloc]initWithFrame:CGRectMake(30, NavigationBarHeight+60, SCREEN_WIDTH-60, 530)];
        _backView.backgroundColor=[UIColor whiteColor];
        _backView.layer.masksToBounds=YES;
        _backView.layer.cornerRadius=5;
    }
    return _backView;
}
-(UIImageView*)headimage
{
    if (!_headimage) {
        _headimage=[[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-45, NavigationBarHeight+15, 90, 90)];
//        _headimage.backgroundColor=[UIColor redColor];
        _headimage.layer.masksToBounds=YES;
        _headimage.layer.cornerRadius=5;
    }
    return _headimage;
}
-(UILabel*)namelbl
{
    if (!_namelbl) {
        _namelbl=[self CreateLabelWithFrame:CGRectMake(10, 80, _backView.width-20, 20) Font:19 titleColor:KHEXCOLOR(@"#262B2F")];
        _namelbl.text=@"小分队";
    }
    return _namelbl;
}
-(UILabel*)titlelbl
{
    if (!_titlelbl) {
        _titlelbl=[self CreateLabelWithFrame:CGRectMake(10, _namelbl.bottom+15, _backView.width-20, 20) Font:13 titleColor:KHEXCOLOR(@"#AFAFAF")];
        _titlelbl.text=@"签名：这个群主很懒，什么都没留下…";
    }
    return _titlelbl;
}


-(UILabel*)groupMangerlbl
{
    if (!_groupMangerlbl) {
        _groupMangerlbl=[self CreateLabelWithFrame:CGRectMake(10, _titlelbl.bottom+75, _backView.width-20, 20) Font:15 titleColor:KHEXCOLOR(@"#262B2F")];
        _groupMangerlbl.text=@"群主";
        _groupMangerlbl.textAlignment=1;
    }
    return _groupMangerlbl;
}
-(UIImageView*)groupMangerHead
{
    if (!_groupMangerHead) {
        _groupMangerHead=[[UIImageView alloc]initWithFrame:CGRectMake(_backView.width/2-22.5, _groupMangerlbl.bottom+15, 45, 45)];
//        _groupMangerHead.backgroundColor=[UIColor redColor];
        _groupMangerHead.layer.masksToBounds=YES;
        _groupMangerHead.layer.cornerRadius=22.5;
    }
    return _groupMangerHead;
}
-(UILabel*)groupMangerName
{
    if (!_groupMangerName) {
        _groupMangerName=[self CreateLabelWithFrame:CGRectMake(10, _groupMangerHead.bottom+15, _backView.width-20, 15) Font:14 titleColor:KHEXCOLOR(@"#262B2F")];
        _groupMangerName.text=@"我是群主";
        _groupMangerName.textAlignment=1;
    }
    return _groupMangerName;
}

-(UIButton*)ConfirmJoinButton
{
    if (!_ConfirmJoinButton) {
        _ConfirmJoinButton=[UIButton buttonWithType:UIButtonTypeCustom];
        _ConfirmJoinButton.frame=CGRectMake(15, _backView.height-60-40, _backView.width-30, 40);
        _ConfirmJoinButton.backgroundColor=KHEXCOLOR(@"#4795EF");
        _ConfirmJoinButton.layer.masksToBounds=YES;
        _ConfirmJoinButton.layer.cornerRadius=5;
        [_ConfirmJoinButton setTitle:@"确认加入" forState:UIControlStateNormal];
        [_ConfirmJoinButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_ConfirmJoinButton addTarget:self action:@selector(joinGroup:) forControlEvents:UIControlEventTouchUpInside];
        _ConfirmJoinButton.titleLabel.font=[UIFont systemFontOfSize:15];
    }
    return _ConfirmJoinButton;
}
-(UILabel*)CreateLabelWithFrame:(CGRect)Frame Font:(CGFloat)font titleColor:(UIColor*)titleColor
{
    UILabel * lbl=[[UILabel alloc]initWithFrame:Frame];
    lbl.font=[UIFont systemFontOfSize:font];
    lbl.textColor=titleColor;
    lbl.textAlignment=1;
    return lbl;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
